// 引入menu列表接口
import {
    userList,
    userCount
} from '../../../request/api'
const state = {
    userlist: [],
    page: 1,
    size: 2,
    cont: 0
}

const getters = {
    getusers(state) {
        return state.userlist
    },
    // 总条数
    getCont(state) {
        return state.cont
    },
    // 一页有几条
    getSize(state) {
        return state.size
    },
    // 当前页数
    getPage(state) {
        return state.page
    }

}

const mutations = {
    setUserList(state, plydata) {
        state.userlist = plydata
    },
    // 总条数
    setUserCont(state, plydata) {
        state.cont = plydata
    },
    // 当前页
    setUserPage(state, plydata) {
        state.page = plydata
    }
}

const actions = {
    getUserList(context) {
        userList({
                page: context.state.page,
                size: context.state.size
            }

        ).then(res => {
            if (res.code == 200) {
                let list = res.list ? res.list : []
                if (context.state.page != 1 && list.length == 0) {
                
     context.dispatch('getUserPage', context.state.page - 1)
                } 
                     context.commit('setUserList', list)
                
            }

        })
    },
    // 获取总条数
    getContActions(context) {
        userCount().then(res => {
            console.log(res, 1111111111)
            context.state.cont = res.list[0].total
        })
    },
    //封装一个切换页码 并重新获取数据的行动
    getUserPage(context, plydata) {
        //修改页码
        context.commit('setUserPage', plydata)
         //重新调取接口
        context.dispatch('getUserList')
    }
}

export default {
    state,
    mutations,
    actions,
    getters,
    namespaced: true
}